package com.example.a2032.Utils;

import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Vibrator;

import android.text.TextUtils;
import android.util.Log;

import com.example.a2032.activity.ClockNotifyActivity;
import com.example.a2032.entity.JstyleClock;


import java.util.Calendar;


public class AlarmReceiver extends BroadcastReceiver {

    private NotificationManager notificationManager;
    private static final String TAG = "AlarmReceiver";
    private MediaPlayer mediaPlayer;
    private Vibrator vibrator;
    private CharSequence NOTIFICATION_CHANNEL_NAME="Clock";

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        JstyleClock jstyleClock= (JstyleClock) intent.getSerializableExtra(ClockUtils.JSTYLE_CLOCK);
        Log.i(TAG, "onReceive: "+jstyleClock.getHour()+" "+jstyleClock.getMinute());
        int id = (int) jstyleClock.getId();
        String cycle=jstyleClock.getCycle();
        if(isContainToday(cycle))showClockActivity(context,intent);
        //直接设置重复闹钟会导致时间不准确。触发闹钟后往后设置加一天的时间更精确
        ClockUtils.getInstance().setNextClock(context,jstyleClock);

    }

    void showClockActivity(Context context,Intent intent){
        JstyleClock jstyleClock= (JstyleClock) intent.getSerializableExtra(ClockUtils.JSTYLE_CLOCK);

        int clockType = intent.getIntExtra(ClockNotifyActivity.KEY_ClockType, JstyleClock.CLOCK_NORMAL);
        Intent intentClock=new Intent(context, ClockNotifyActivity.class);
        intentClock.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intentClock.putExtra(ClockNotifyActivity.KEY_ClockType, clockType);
        intentClock.putExtra(ClockUtils.JSTYLE_CLOCK, jstyleClock);
        context.startActivity(intentClock);
    }
    boolean isContainToday(String cycle){
        boolean isContainToday=false;
        Log.i(TAG, "isContainToday: "+cycle);
        String[]cycleArray=cycle.split(",");
        Calendar calendar=Calendar.getInstance();
        int currentDays=calendar.get(Calendar.DAY_OF_WEEK);
        //取出来的星期天（1）对应服务器的7
        //其他的都减一
        if(currentDays==1){
            currentDays=7;
        }else{
            currentDays=currentDays-1;
        }
        for(String cycleDays:cycleArray){
            if(TextUtils.isEmpty(cycleDays))continue;
            int days=Integer.valueOf(cycleDays);
            if(currentDays==days){
                isContainToday=true;
                break;
            }
        }
        Log.i(TAG, "isContainToday: "+isContainToday+" "+currentDays);
        return isContainToday;
    }
    private void setAlarms(Context context, String name, int id) {
        Intent intent = new Intent("android.intent.action.ALARM_RECEIVER");
        intent.putExtra("name", name);
        intent.putExtra("_id", id);
        intent.addCategory("android.intent.category.jstyleWatch");
        PendingIntent sender = PendingIntent.getBroadcast(context, id, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH,
                calendar.get(Calendar.DAY_OF_MONTH) + 1);
        calendar.set(Calendar.SECOND,
                0);
        Log.i(TAG, "setAlarms: " + calendar.getTime().toString());
        am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
    }




}
